package com.kgc.kmall.kmallsearchweb.controller;

import com.kgc.kmall.annotations.LoginRequired;
import com.kgc.kmall.bean.*;
import com.kgc.kmall.service.AttrService;
import com.kgc.kmall.service.SearchService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
public class SearchController {
    @Reference
    SearchService searchService;

    @Reference
    AttrService attrService;

    @LoginRequired(false)
    @RequestMapping("/index.html")
    public String index(){        return "index";}

    @RequestMapping("/list.html")
    public String list(PmsSearchSkuParam pmsSearchSkuParam, Model model){
        List<PmsSearchSkuInfo> list = searchService.list(pmsSearchSkuParam);
        //获取平台属性valueId
        Set<Long> valueIdSet=new HashSet<>();
        for (PmsSearchSkuInfo pmsSearchSkuInfo : list) {
            for (PmsSkuAttrValue pmsSkuAttrValue : pmsSearchSkuInfo.getSkuAttrValueList()) {
                valueIdSet.add(pmsSkuAttrValue.getValueId());
            }
        }
        List<PmsBaseAttrInfo> pmsBaseAttrInfos = attrService.selectAttrInfoValueListByValueId(valueIdSet);
        model.addAttribute("attrList",pmsBaseAttrInfos);
        model.addAttribute("skuLsInfoList",list);


        //根据参数独享拼接url
        StringBuffer stringBuffer=new StringBuffer();
        String catalog3Id = pmsSearchSkuParam.getCatalog3Id();
        String keyword = pmsSearchSkuParam.getKeyword();
        String[] skuAttrValueList = pmsSearchSkuParam.getValueId();
            if(StringUtils.isNotBlank(catalog3Id)){
                stringBuffer.append("&catalog3Id="+catalog3Id);
            }
            if(StringUtils.isNotBlank(keyword)){
                stringBuffer.append("&keyword="+keyword);
            }
            if(skuAttrValueList!=null){
                for (String s : skuAttrValueList) {
                    stringBuffer.append("&valueId="+s);
                }
            }
        String substring = stringBuffer.substring(1);
        model.addAttribute("urlParam",substring);



        //回显那个面包屑  关键字
        model.addAttribute("keyword",pmsSearchSkuParam.getKeyword());
        //封装面包屑
        if(skuAttrValueList!=null){
         List<PmsSearchCrumb> pmsSearchCrumbs=new ArrayList<>();
            for (String s : skuAttrValueList) {
                PmsSearchCrumb pmsSearchCrum=new PmsSearchCrumb();
                String urlParam = getUrlParam(pmsSearchSkuParam, s);
                pmsSearchCrum.setUrlParam(urlParam);
                pmsSearchCrum.setValueId(s);
                String valueName = getValueName(pmsBaseAttrInfos, s);
                pmsSearchCrum.setValueName(valueName);
                pmsSearchCrumbs.add(pmsSearchCrum);
            }
         model.addAttribute("attrValueSelectedList",pmsSearchCrumbs);
        }



        //已选中的valueId移除
        if(skuAttrValueList!=null){
            Iterator<PmsBaseAttrInfo> iterator = pmsBaseAttrInfos.iterator();
            while (iterator.hasNext()){
                PmsBaseAttrInfo pmsBaseAttrInfo=iterator.next();
                for (PmsBaseAttrValue pmsBaseAttrValue : pmsBaseAttrInfo.getAttrValueList()) {
                    for (String s : skuAttrValueList) {
                        if(s.equals(pmsBaseAttrValue.getId().toString())){
                            iterator.remove();
                        }
                    }
                }
            }
        }


        return "list";
    }




    //根据参数对象拼接url
    public String getUrlParam(PmsSearchSkuParam pmsSearchSkuParam,String valueId){
        //根据参数独享拼接url
        StringBuffer stringBuffer=new StringBuffer();
        String catalog3Id = pmsSearchSkuParam.getCatalog3Id();
        String keyword = pmsSearchSkuParam.getKeyword();
        String[] skuAttrValueList = pmsSearchSkuParam.getValueId();
        if(StringUtils.isNotBlank(catalog3Id)){
            stringBuffer.append("&catalog3Id="+catalog3Id);
        }
        if(StringUtils.isNotBlank(keyword)){
            stringBuffer.append("&keyword="+keyword);
        }
        if(skuAttrValueList!=null){
            for (String s : skuAttrValueList) {
                if(s.equals(valueId)==false){
                    stringBuffer.append("&valueId="+s);
                }
            }
        }
        String substring = stringBuffer.substring(1);
        return substring;
    }

    //根据valueId查询valueName
    public String getValueName(List<PmsBaseAttrInfo> pmsBaseAttrInfos,String valueId){
        String valueName="";
        for (PmsBaseAttrInfo pmsBaseAttrInfo : pmsBaseAttrInfos) {
            for (PmsBaseAttrValue pmsBaseAttrValue : pmsBaseAttrInfo.getAttrValueList()) {
                if(valueId.equals(pmsBaseAttrValue.getId().toString())){
        valueName=pmsBaseAttrInfo.getAttrName()+":"+pmsBaseAttrValue.getValueName();

                }
            }
        }
        return valueName;
    }


}
